/*
 * Delete.java
 *
 * Created on 2008年6月9日, 下午9:04
 */

package gui;

import abstractmodel.IBook;
import abstractmodel.IUser;

/**
 *
 * @author  yux
 */
public class Delete extends javax.swing.JPanel {
    Object owner;
    int type;
    /** Creates new form Delete */
    public Delete(Object o, int t) {
        initComponents();
        type = t;
        owner = o;
        if (type == configure.Global.USER_INFO_TYPE) {
            this.jLabelInfo.setText("删除当前用户？");
        }else if(type == configure.Global.BOOK_INFO_TYPE) {
            this.jLabelInfo.setText("删除这个账簿？");
        }else if(type == configure.Global.BANK_ACCOUNT_INFO_TYPE
                || type == configure.Global.CREDITCARD_ACCOUNT_INFO_TYPE
                || type == configure.Global.MONEY_ACCOUNT_INFO_TYPE) {
            this.jLabelInfo.setText("删除这个帐户？");
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabelInfo = new javax.swing.JLabel();
        jButtonConfirm = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        jLabelInfo.setFont(new java.awt.Font("宋体", 0, 14));

        jButtonConfirm.setText("确定");
        jButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonConfirmActionPerformed(evt);
            }
        });

        jButtonCancel.setText("取消");
        jButtonCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(52, 52, 52)
                .addComponent(jButtonConfirm)
                .addGap(48, 48, 48)
                .addComponent(jButtonCancel)
                .addContainerGap(74, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(65, Short.MAX_VALUE)
                .addComponent(jLabelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(62, 62, 62))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addComponent(jLabelInfo, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonConfirm)
                    .addComponent(jButtonCancel))
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonConfirmActionPerformed
        // TODO add your handling code here:
        if (type == configure.Global.USER_INFO_TYPE) {
            //delete user
            IUser ur = (IUser)owner;
            Main.mf.deleteUser(ur);
        }else if(type == configure.Global.BOOK_INFO_TYPE) {
            //delete book
            IBook book = (IBook)owner;
            try {
                book.getOwnerUser().deleteBook(book);
            }catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }else if(type == configure.Global.BANK_ACCOUNT_INFO_TYPE
                || type == configure.Global.CREDITCARD_ACCOUNT_INFO_TYPE
                || type == configure.Global.MONEY_ACCOUNT_INFO_TYPE) {
            //delete BANK_ACCOUNT
            model.BankAccount account = (model.BankAccount)owner;
            account.getOwnerBook().deleteAccount(account);
        }
        jButtonCancelActionPerformed(evt);
    }//GEN-LAST:event_jButtonConfirmActionPerformed

    private void jButtonCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCancelActionPerformed
        // TODO add your handling code here:
        Utilities.cancelDialog(this);
    }//GEN-LAST:event_jButtonCancelActionPerformed
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonConfirm;
    private javax.swing.JLabel jLabelInfo;
    // End of variables declaration//GEN-END:variables
    
}
